/*
 ===============================================================================
 Name        : main.cpp
 Author      : Jason
 Version     : 1.0.0
 Date		 : 2012/9/18
 Copyright   :
 License	 : CC BY-SA 3.0
 Description :
 ===============================================================================
  	 	 	 	 	 	 	 	 History
 ---------+---------+--------------------------------------------+-------------
 DATE     |	VERSION |	DESCRIPTIONS							 |	By
 ---------+---------+--------------------------------------------+-------------

 ===============================================================================
 */
#include "driver_config.h"
#ifdef __USE_CMSIS
#include "LPC11xx.h"
#endif

#include <cr_section_macros.h>
#include <NXP/crp.h>

// Variable to store CRP value in. Will be placed automatically
// by the linker when "Enable Code Read Protect" selected.
// See crp.h header for more information
__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;

// TODO: insert other include files here
#include "gpio.h"
#include "rtos.h"

// TODO: insert other definitions and declarations here

// LPCXpresso processor card LED
#define LED1_PORT	0		// Port for led
#define LED1_BIT	7		// Bit on port for led
#define LED2_PORT	3
#define LED2_BIT	3
#define LED_ON 		1		// Level to set port to turn on led
#define LED_OFF 	0		// Level to set port to turn off led

//
// LED1 Task
//
void vTask_LED1(void *pvParameters) {
	int nLedStatus = 0;
	while(1) {
		if ( nLedStatus ) {
			GPIOSetValue( LED1_PORT, LED1_BIT, LED_ON );
		} else {
			GPIOSetValue( LED1_PORT, LED1_BIT, LED_OFF );
		}
		nLedStatus = !nLedStatus;
		vTaskDelay(500/portTICK_RATE_MS);
	}
}

//
// LED2 Task
//
void vTask_LED2(void *pvParameters) {
	int nLedStatus = 0;
	while(1) {
		if ( nLedStatus ) {
			GPIOSetValue( LED2_PORT, LED2_BIT, LED_ON );
		} else {
			GPIOSetValue( LED2_PORT, LED2_BIT, LED_OFF );
		}
		nLedStatus = !nLedStatus;
		vTaskDelay(200/portTICK_RATE_MS);
	}
}

int main(void) {
	// Setup hardware
	GPIOInit();

	// Set port for LED to output
	GPIOSetDir(LED1_PORT, LED1_BIT, 1);	// for LED1
	GPIOSetDir(LED2_PORT, LED2_BIT, 1);	// for LED2

	// Start LED1 Task
	xTaskCreate(
			vTask_LED1,					// task function
			( signed char * )"led",		// task name
			configMINIMAL_STACK_SIZE,	// task stack size
			NULL,						// task parameters
			tskIDLE_PRIORITY,			// task priority
			NULL );						// task handle

	// Start LED2 Task
	xTaskCreate(
			vTask_LED2,					// task function
			( signed char * )"led",		// task name
			configMINIMAL_STACK_SIZE,	// task stack size
			NULL,						// task parameters
			tskIDLE_PRIORITY,			// task priority
			NULL );						// task handle

	/* Start the tasks and timer running. */
	vTaskStartScheduler();

	return 0 ;
}

//
// FreeRTOS Hook Functions
//
void vApplicationIdleHook() {
	// TODO: FreeRTOS Idle Hook
}

void vApplicationTickHook() {
	// TODO: FreeRTOS Tick Hook
}

void vApplicationMallocFailedHook() {
	// TODO: Faile malloc Hook
}

